﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ServiceModel;
using SLServiceLibrary;

namespace ClientApplication
{
    public partial class ServiceMng : Form
    {
        private ServiceHost host = null;

        private bool serviceStat = false;

        public ServiceMng()
        {
            InitializeComponent();
        }
        #region 启动/停止按钮点击事件处理
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_start_Click(object sender, EventArgs e)
        {
            this.backgroundWorker_open.RunWorkerAsync();
            //try
            //{
            //    if (host != null)
            //        host.Close();
            //    host = new ServiceHost(typeof(GISService));
            //    host.Open();
            //    //this.textBox1.Text = "Server Opened!";
            //    label.Text = "Started";
            //    label.ForeColor = Color.Green;
            //    serviceStat = true;
            //}
            //catch (Exception ex)
            //{
            //    MessageBox.Show(ex.ToString());
            //    if (host != null)
            //    {
            //        host.Close();
            //        serviceStat = false;
            //    }
            //}
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_stop_Click(object sender, EventArgs e)
        {

            if (host != null)
            {
                host.Close();
                label.Text = "Stopped";
                label.ForeColor = Color.Red;
                serviceStat = false;
            }
        }
        #endregion


        #region 窗体最小化到系统托盘

        private void ServiceMng_SizeChanged(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                this.Hide();
                this.notifyIcon1.Visible = true;
            }
        }
        private void notifyIcon1_Click(object sender, EventArgs e)
        {
            this.Visible = true;
            this.WindowState = FormWindowState.Normal;
            this.notifyIcon1.Visible = false;
        }
        #endregion


       
        /// <summary>
        /// 窗体关闭按钮点击事件处理函数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ServiceMng_FormClosing(object sender, FormClosingEventArgs e)
        {
            //窗体关闭时间执行代码
            //提示是否进行关闭，不建议关闭
            //提示进行最小化到状态栏
            //如进行关闭，提示先停止预测服务
            if (MessageBox.Show("确认关闭预测服务管理界面吗？\n\n关闭请点击YES，不关闭并最小化到状态栏请点击NO", "确认关闭预测服务管理界面", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
            {
                //进行关闭操作，检查服务状态
                if (serviceStat)
                {
                    if (MessageBox.Show("确认关闭预测服务吗？\n\n关闭请点击YES，不关闭请点击NO", "确认关闭预测服务", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
                    {
                        this.host.Close();
                        this.Dispose();
                    }
                    else
                    {
                        MessageBox.Show("建议最小化预测服务管理界面到系统托盘");
                        e.Cancel = true;
                    }
                }
                //Application.Exit();
            }
            else
            {
                e.Cancel = true;
                //
                this.WindowState = FormWindowState.Minimized;
                this.Hide();
                this.notifyIcon1.Visible = true;
            } 
        }
        #region 多线程启动wcf服务
        private void backgroundWorker_open_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                if (host != null)
                    host.Close();
                host = new ServiceHost(typeof(GISService));
                host.Open();
                //this.textBox1.Text = "Server Opened!";

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                if (host != null)
                {
                    host.Close();
                    serviceStat = false;
                }
            }
        }

        private void backgroundWorker_open_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            label.Text = "Started";
            label.ForeColor = Color.Green;
            serviceStat = true;
        }
        #endregion


    }
}
